package com.hydl.power.controller;


import com.github.pagehelper.PageInfo;
import com.hydl.power.aop.annotation.LogAnnotation;
import com.hydl.power.pojo.SysMenu;
import com.hydl.power.pojo.SysMenuVO;
import com.hydl.power.pojo.SysUser;
import com.hydl.power.service.SysUserRoleService;
import com.hydl.power.service.SysUserService;
import com.hydl.power.utils.MD5Util;
import com.hydl.power.utils.Resp;
import com.hydl.power.utils.TableUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;


/**
 * @author Administrator
 */
@RestController
@RequestMapping("/login")
@Api(value = "LoginController", tags = "登录")
@CrossOrigin
public class LoginController {

    @Autowired
    private SysUserService userService;
    @Autowired
    private SysUserRoleService sysUserRoleService;


    @LogAnnotation(module = "用户模块",description = "登录")
    @PostMapping("/login")
    @ApiOperation(value = "登录", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "userName", value = "用户名", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "password", value = "密码", required = true, dataType = "String"),
    })
    public Resp login(String userName, String password) {
        if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)) {
            return Resp.fail("不能为空");
        }
        SysUser user = userService.getUser(userName);
        if (user == null || !user.getPassword().equals(MD5Util.crypt(password))) {
            return Resp.fail("密码错误");
        }
        userService.updateLoginTime(user.getUsername());
        //页面
        return Resp.success(user);
    }

    @PostMapping("/toLogin")
    public String toLogin(Integer error, HttpServletRequest request) {
        if (error != null) {
            request.setAttribute("error", error);
        }
        //页面
        return "/login";
    }

    //@LogAnnotation(module = "用户模块",description = "退出")
    @PostMapping("/logout")
    @ApiOperation(value = "登出", produces = "application/json")
    public String logout(HttpServletRequest request) {
        request.getSession().removeAttribute("USER_SESSION");
        //页面
        return "/login";
    }

    @PostMapping("/updatePassword")
    @ApiOperation(value = "修改密码", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "password", value = "旧密码", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "password1", value = "新密码", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "password2", value = "重复密码", required = true, dataType = "String"),
    })
    public Resp updateAccount(String password, String password1, String password2,String token) {
        if (StringUtils.isEmpty(password) || StringUtils.isEmpty(password1) || StringUtils.isEmpty(password2) || !password1.equals(password2)) {
            return Resp.fail("修改失败");
        }
        SysUser  user=userService.getUserByToken(token);
        if (!user.getPassword().equals(MD5Util.crypt(password))) {
            return Resp.fail("旧密码不正确");
        }
        int i = userService.updatePassword(user.getId(), MD5Util.crypt(password1));
        return Resp.success("修改成功");
    }

    //@LogAnnotation(module = "用户模块",description = "菜单")
    @PostMapping("/menu/get")
    @ApiOperation(value = "菜单", produces = "application/json")
    public Resp<LinkedHashSet<SysMenuVO>> getMenus(Integer userId) {
       /* SysUser user = (SysUser) request.getSession().getAttribute("USER_SESSION");
        if (user == null){
            return Resp.fail("用户数据为空");
        }*/
        Set<SysMenuVO> set = userService.getMenu(userId);
        return Resp.success(set);
    }

    @LogAnnotation(module = "用户模块",description = "添加用户")
    @PostMapping(value = "/addUser")
    @ApiOperation(value = "用户增加", produces = "application/json")
    @Transactional
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "userName", value = "用户名", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "card", value = "工号", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "项目id", required = false, dataType = "Integer"),
    })
    public Resp insert(String userName, String card,Integer projectId){
        SysUser sysUserList = userService.getUser(userName);
        if (sysUserList != null){
            return Resp.fail("该用户名已存在");
        }
      Integer status=2;
      Integer mapStatus=2;
      Integer roleId=4;

        int i = userService.addUser(userName,card,status,projectId,mapStatus);
        if (i == 1){
            SysUser user=userService.getUser(userName);
            int a=sysUserRoleService.addSysUserRole(user.getId(),roleId);
            if(a == 1){
                return Resp.success("Success");
            }
        }
        return Resp.fail("添加失败");
    }


    @LogAnnotation(module = "用户模块",description = "用户注册")
    @PostMapping(value = "/register")
    @ApiOperation(value = "用户注册", produces = "application/json")
    @Transactional
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "userName", value = "用户名", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "card", value = "工号", required = true, dataType = "String"),
    })
    public Resp register(String userName, String card ){
        Integer status=2;
        Integer mapStatus=2;
        Integer projectId=null;
        Integer roleId=4;
        SysUser sysUserList = userService.getUser(userName);
        if (sysUserList != null){
            return Resp.fail("该用户名已存在");
        }
        int i = userService.addUser(userName,card,status,projectId,mapStatus);
        if (i == 1){
            SysUser user=userService.getUser(userName);
            int a=sysUserRoleService.addSysUserRole(user.getId(),roleId);
            if(a == 1){
                return Resp.success("Success");
            }
        }
        return Resp.fail("注册失败");
    }

    //@LogAnnotation(module = "用户模块",description = "用户管理list分页")
    @PostMapping(value = "/getAllSysUser")
    @ApiOperation(value = "用户管理list分页",produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNum", value = "页数", dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "条数",  dataType = "Integer"),
    })
    public Resp<TableUtils> getAllUser(Integer pageNum, Integer pageSize){
        return Resp.success(userService.getList(pageNum, pageSize));
    }

    @LogAnnotation(module = "用户模块",description = "根据ID删除用户")
    @ApiOperation(value="根据ID删除用户")
    @PostMapping("/deleteUserById")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "用户id", dataType = "Integer"),
    })
    public Resp deleteUser(Integer id){
        int i =  userService.deleteUserById(id);
        if (i == 1){
            return Resp.success("Success");
        }else{
            return Resp.fail("删除失败");
        }
    }

    @LogAnnotation(module = "用户模块",description = "修改用户")
    @ApiOperation(value="根据ID修改用户")
    @PostMapping("/updateUserById")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "用户id", dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "username", value = "用户名", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "card", value = "工号", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "flag", value = "1 启用 2不启用", dataType = "Integer"),
    })
    public Resp updateUser(Integer id,String username,String card,Integer flag){
        int i =  userService.updateUserById(id,username,card,flag);
        if (i == 1){
            return Resp.success("Success");
        }else{
            return Resp.fail("修改失败");
        }
    }

    //@LogAnnotation(module = "用户模块",description = "根据ID获取用户")
    @ApiOperation(value="根据ID获取用户")
    @PostMapping("/getUserById")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "用户id", dataType = "Integer"),
    })
    public Resp getAllById(Integer id){
        return Resp.success(userService.getAllById(id));
    }
}
